# -*- coding: utf-8 -*-
# @Time    : 2019/12/16 9:52
# @Author  : Damn7Kx
# @Software: PyCharm
import re
from pyquery import PyQuery as pq
from scrapy.selector import Selector
from w3lib.html import remove_tags


def parse_weixin(html):
    doc = pq(html)
    # 将a连接删除
    doc('a').remove()
    # 使用删除后html的进行css选择
    res = Selector(text=str(doc))
    content = res.css('#js_content').extract_first()
    # title = res.css('#activity-name').extract_first()
    # new_title = remove_tags(title).strip()
    new_content = ''.join(remove_tags(content).split())
    try:
        author = extract_author(new_content)
    except:
        author = ''
    pubdate = get_weixin_pubdate(html)
    return author,pubdate,new_content,html


# 提取作者信息
def extract_author(text):
    pattern = re.compile('[\s\S]+[作者|编辑|记者|责编|来源][:|：].*?([\S\s][\u4E00-\u9FA5]+)', re.S)
    match_re = re.search(pattern, text)
    return match_re.group(1)


# 提取时间 借鉴github
def extract_pubtime(div_text):
    pubtime = ''
    pubtime = re.search(
        r'(\d{4}\s*[年\-:/]\s*)\d{1,2}\s*[月\-：/]\s*\d{1,2}\s*[\-_:日]?\s*\d{1,2}\s*:\s*\d{1,2}\s*(:\s*\d{1,2})?',
        div_text, flags=re.S | re.I)
    if pubtime:
        pubtime = pubtime.group()
    if not pubtime:
        pubtime = re.search(r'(\d{4}\s*[年\-:/]\s*)\d{1,2}\s*[月\-：/]\s*\d{1,2}\s*[\-_:日/]?', div_text,
                            flags=re.S)
        if pubtime:
            pubtime = pubtime.group()
    if pubtime:
        pubtime = pubtime.strip()
        pubtime = pubtime.replace('年', '-').replace('月', '-').replace('日', ' ').replace('/', '-')
        pubtime = drop_mutil_blank(pubtime)
        return pubtime
    else:
        return pubtime


def get_weixin_pubdate(html):
    # 匹配出时间戳
    result = re.search('var ct = "(\d+)";',html)
    return result.group(1)

def drop_mutil_blank(str):
    str = re.sub(r'\s{2,}', ' ', str)
    return str


if __name__ == '__main__':
    pass